const db = wx.cloud.database()
const _ = db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    college:[],
    collegeCur: -2,
    showList: false,
    scrollTop: 0,
    nomore: false,
    list: [],
    collegeNow:"all",
    page:0
  },

  //监测屏幕滚动
  onPageScroll: function (e) {
      this.setData({
            scrollTop: parseInt((e.scrollTop) * wx.getSystemInfoSync().pixelRatio)
      })
  },

   //刷新
   onRefresh(){
      //在当前页面显示导航条加载动画
      wx.showNavigationBarLoading(); 
      //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
      wx.showLoading({
        title: '刷新中...',
      })
      if(this.data.collegeCur === -2){
            this.getAllList();
          }else{
                this.getList();
            }
      setTimeout(function () {
            //隐藏loading 提示框
       wx.hideLoading();
       //隐藏导航条加载动画
       wx.hideNavigationBarLoading();
       //停止下拉刷新
       wx.stopPullDownRefresh();
      }, 1000)
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    db.collection('college').get({
      success:function(res){
        // console.log('获取学院分类成功',res.data)
        that.setData({
          college:res.data
        })
      }
    })
    this.listkind();
    //this.getbanner();
      //   wx.cloud.callFunction({
      //     name:'getOpenid',
      //     success(res){
      //       console.log('获取openid成功',res);
      //       that.setData({
      //         openid:res.result.openid
      //       })
      //       wx.setStorageSync("openid", res.result.openid);
      //     },
      //     fail(err){
      //       console.log('获取openid失败',err);
      //     }
      //   })
        wx.getStorage({
              key:'openid',
              success(res){
                    that.setData({
                          openid:res.data
                    })
              },
              fail(res){
                    console.log('fail',res)
                    wx.showModal({
                        title: '温馨提示',
                        content: '登录后才能体验完整功能喔，是否马上去登录',
                        success(res) {
                              if (res.confirm) {
                                    wx.switchTab({
                                      url:'../my/my',
                                    })
                              }
                        }
                    })
              }
      })
  },

  //获取上次布局记忆
  listkind() {
      let that = this;
      wx.getStorage({
            key: 'iscard',
            success: function(res) {
                  that.setData({
                        iscard: res.data
                  })
            },
            fail() {
                  that.setData({
                        iscard: true,
                  })
            }
      })
  },
  //布局方式选择
  changeCard() {
      let that = this;
      if (that.data.iscard) {
            that.setData({
                  iscard: false
            })
            wx.setStorage({
                  key: 'iscard',
                  data: false,
            })
      } else {
            that.setData({
                  iscard: true
            })
            wx.setStorage({
                  key: 'iscard',
                  data: true,
            })
      }
  },
  //跳转搜索
  search() {
      wx.navigateTo({
            url: '/pages/search/search',
      })
  },
  //学院选择
  collegeSelect(e) {
    //console.log('e',e)
      this.setData({
            collegeCur: e.currentTarget.dataset.id - 1,
            scrollLeft: (e.currentTarget.dataset.id - 3) * 100,
            showList: false,
            collegeNow:e.currentTarget.dataset.name
      })
      this.getList();
  },
  //选择全部
  selectAll() {
      this.setData({
            collegeCur: -2,
            scrollLeft: -200,
            showList: false,
            collegeNow:'all'
      })
      this.getAllList();
  },
  //展示列表小面板
  showlist() {
      let that = this;
      if (that.data.showList) {
            that.setData({
                  showList: false,
            })
      } else {
            that.setData({
                  showList: true,
            })
      }
  },

  getAllList(){
    let that = this
    db.collection('books').where({
      status: 0,
      }).orderBy('time','desc').orderBy('price', 'asc').limit(20).get({
      success: function(res) {
            wx.stopPullDownRefresh(); //暂停刷新动作
            console.log('成功获取该分类的数据：',res.data)
            if (res.data.length == 0) {
                  that.setData({
                        nomore: true,
                        list: [],
                  })
                  return false;
            }
            if (res.data.length < 20) {
                  that.setData({
                        nomore: true,
                        page: 0,
                        list: res.data,
                  })
            } else {
                  that.setData({
                        page: 0,
                        list: res.data,
                        nomore: false,
                  })
            }
      }
    })
  },

  getList() {
      let that = this
      // if (that.data.collegeCur == -2) {
      //       var collegeid = _.neq(-2); //除-2之外所有
      // } else {
      //       var collegeid = that.data.collegeCur + '' //小程序搜索必须对应格式
      // }
      db.collection('books').where({
            status: 0,
            type:that.data.collegeNow
      }).orderBy('time','desc').orderBy('price', 'asc').limit(20).get({
            success: function(res) {
                  wx.stopPullDownRefresh(); //暂停刷新动作
                  console.log('成功获取该分类的数据：',res.data.length)
                  if (res.data.length == 0) {
                        that.setData({
                              nomore: true,
                              list: [],
                        })
                        return false;
                  }
                  if (res.data.length < 20) {
                        that.setData({
                              nomore: true,
                              page: 0,
                              list: res.data,
                        })
                  } else {
                        that.setData({
                              page: 0,
                              list: res.data,
                              nomore: false,
                        })
                  }
            }
      })
  },
  more() {
      let that = this;
      if (that.data.nomore || that.data.list.length < 20) {
            return false
      }
      let page = that.data.page + 1;
      // if (that.data.collegeCur == -2) {
      //       var collegeid = _.neq(-2); //除-2之外所有
      // } else {
      //       var collegeid = that.data.collegeCur + '' //小程序搜索必须对应格式
      // }
      if(that.data.collegeNow == 'all'){
        db.collection('books').where({
          status: 0,
          }).orderBy('time','desc').orderBy('price', 'asc').skip(page * 20).limit(20).get({
            success: function(res) {
                  if (res.data.length == 0) {
                        that.setData({
                              nomore: true
                        })
                        return false;
                  }
                  if (res.data.length < 20) {
                        that.setData({
                              nomore: true
                        })
                  }
                  that.setData({
                        page: page,
                        list: that.data.list.concat(res.data)
                  })
            },
            fail() {
                  wx.showToast({
                        title: '获取失败',
                        icon: 'none'
                  })
            }
          })
      }
      else{
        db.collection('books').where({
          status: 0,
          type:that.data.collegeNow
          }).orderBy('time','desc').orderBy('price', 'asc').skip(page * 20).limit(20).get({
          success: function(res) {
                if (res.data.length == 0) {
                      that.setData({
                            nomore: true
                      })
                      return false;
                }
                if (res.data.length < 20) {
                      that.setData({
                            nomore: true
                      })
                }
                that.setData({
                      page: page,
                      list: that.data.list.concat(res.data)
                })
          },
          fail() {
                wx.showToast({
                      title: '获取失败',
                      icon: 'none'
                })
          }
        })
      }
  },

  onReachBottom() {
      this.more();
  },
  //下拉刷新
//   onPullDownRefresh() {
//       this.getList();
//   },
  gotop() {
      wx.pageScrollTo({
            scrollTop: 0
      })
  },
  //跳转详情
  detail(e) {
      let that = this;
      wx.navigateTo({
            url: '/pages/detail/detail?scene=' + e.currentTarget.dataset.id,
      })
  },
  //获取轮播
//   getbanner() {
//       let that = this;
//       db.collection('banner').where({}).get({
//             success: function(res) {
//                   console.log('resbanner',res)
//                   that.setData({
//                         banner: res.data[0].list
//                   })
//             }
//       })
//   },
  //跳转轮播链接
//   goweb(e) {
//       if (e.currentTarget.dataset.web){
//             wx.navigateTo({
//                   url: '/pages/web/web?url='+e.currentTarget.dataset.web.url,
//             })
//       }
//   },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAllList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //调用刷新时将执行的方法
    this.onRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.more()
  },

  /**
   * 用户点击右上角分享
   */
    // onShareAppMessage() {
    //   return {
    //         title: JSON.parse(config.data).share_title,
    //         imageUrl: JSON.parse(config.data).share_img,
    //         path: '/pages/start/start'
    //   }
    // },
})